from django.db import models
from django.contrib.auth.models import AbstractUser
from blog.models import Article
# Create your models here.


class UserInfo(AbstractUser):
    # 使用Django自带的user字段，新增自定义字段
    id = models.AutoField(primary_key=True)
    nickname = models.CharField(verbose_name='昵称', max_length=32)
    phone = models.CharField(verbose_name='手机号', max_length=11, null=True, unique=True)
    avatar = models.ImageField(verbose_name='头像', upload_to='tx/', default='tx/default.png')

    def __str__(self):
        return self.username


class Link(models.Model):
    # 友情链接
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100, verbose_name='标题')
    url = models.CharField(max_length=200, verbose_name='连接',
                           null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    class Meta:
        verbose_name_plural = verbose_name = '友情链接'
        ordering = ['-create_time']

    def __str__(self):
        return self.title


class Like(models.Model):
    # 点赞
    id = models.AutoField(primary_key=True)
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    num = models.IntegerField(verbose_name='点赞数', default=0)
    # user = models


class AD(models.Model):
    STATUS = {
    0: '正常',
    1: '删除',
}
    # 广告
    url = models.URLField(verbose_name='链接地址', max_length=200)
    img = models.ImageField(verbose_name ='图片',  upload_to='ad/', default='ad/default.png')
    status = models.SmallIntegerField(default=0, choices=STATUS.items(), verbose_name='状态')
    desc = models.TextField(verbose_name="描述", max_length=500)
    
    def __str__(self):
        return self.url

    class Meta:
        verbose_name_plural = verbose_name = '广告'
